package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartInfoService cartInfoService;


    @ApiOperation("添加购物车")
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum,
                            HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
            if (StringUtils.isEmpty(userId)) {
                return Result.fail().message("系统异常");
            }
        }

        cartInfoService.addToCart(skuId, skuNum, userId);
        return Result.ok();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/cartList")
    public Result<List<CartInfo>> getCartInfoList(HttpServletRequest request) {

        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);

        List<CartInfo> cartList = cartInfoService.getCartList(userId, userTempId);

        return Result.ok(cartList);
    }

    @ApiOperation("更改选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result<Void> checkCart(@PathVariable("skuId") String skuId,
                                  @PathVariable("isChecked") Integer isChecked,
                                  HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.updateIsCheck(skuId, isChecked, userId);
        return Result.<Void>ok();
    }

    @ApiOperation("删除单个订单")
    @DeleteMapping("/deleteCart/{skuId}")
    public Result<Void> deleteCart(@PathVariable("skuId") String skuId, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCart(skuId, userId);
        return Result.<Void>ok();
    }

    @ApiOperation("删除所有已购买的订单")
    @DeleteMapping("/delete/all/checked/cart")
    public Result<Void> deleteAll(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        cartInfoService.deleteAll(userId);
        return Result.<Void>ok();
    }

    @GetMapping("/auth/get/checked/cart")
    public Result<List<CartInfo>> getCheckedCart(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if (StringUtils.isEmpty(userId)) {
            return Result.<List<CartInfo>>fail().message("没有获取到有效 userId！");
        }

        List<CartInfo> cartInfoList = cartInfoService.getCheckedCart(userId);

        return Result.ok(cartInfoList);
    }
}
